var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate;

layui.use(['tableEdit',"excel"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel;

    lay('.starttime').each(function () {
        laydate.render({
            elem: this
        });
    });

    function getAllJson(jsons) {
        let tmpitem = []
        for (let item of jsons) {
            let tmparr = {}
            tmparr["value"] = item["code"];
            tmparr["name"] = item["name"];
            if (item["users"]) {
                for (let it of item["users"]) {
                    item["list"].push(it)
                }
            }
            if (item["list"] && item["list"].length) {
                tmparr["children"] = getAllJson(item["list"]);
                tmpitem.push(tmparr)
            } else {
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }

    function getxmselect(el, name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon: true,
                indent: 20,
                expandedKeys: false
            },
            height: 'auto',
            data: data,
            on: function ({arr, change, isAdd}) {
                return this
            }
        })
    }


    let warehouseList = [];
    let operationList=[];
    layer.ready(function () {
        //获取仓库信息
        $.ajax({
            url:"transfer/getWarehouse",
            success:function(data){
                if(data.status == 200){
                    let warehouse = [];
                    for(let i in data.data){
                        let tmp = {
                            name:data.data[i].name,
                            value:data.data[i].code
                        }
                        warehouse.push(tmp);
                    }
                    warehouseList = getxmselect("#warehouseCode", "warehouseCode", warehouse);
                    form.render('select');
                }
            }
        });

        //获取当前用户的部门及人员结构树
        let treeList = getCurrentDepartmentUser();

        if (treeList && treeList.list) {
            let userTree = getAllJson(treeList.list);
            operationList=getxmselect("#operationNo", "operationNo", userTree);
        } else {
            operationList=getxmselect("#operationNo", "operationNo", [{name: user.name, value: user.code}])
        }

    });
    form.render();//表单刷新

    let initData = [];
    table.render({
        elem: '#offerselect'
        , data: initData
        , toolbar: '#offertoolbar'
        ,totalRow: true
        , cols: [[
            //以外销合同主表为主
            {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'}
            , {field: 'billNo', title: '入库单号', sort: true, width: 220, event: "quotationno"}
            , {field: 'createDate', title: '创建时间', sort: true, width: 120}
            , {field: 'lotNo', title: '批次号', sort: true, width: 220}
            , {field: 'warehouseName', title: '仓库名称', sort: true, width: 120}
            , {field: 'productCode', title: '产品编码', sort: true, width: 220}
            , {field: 'specificationCode', title: '规格编码', sort: true, width: 120}
            , {field: 'productNum', title: '产品数量', sort: true, width: 120}
            , {field: 'productUnitPrice', title: '成本单价', sort: true, width: 120}
            , {field: 'productCname', title: '产品中文名称', sort: true, width: 120}
            , {field: 'productEname', title: '产品英文名称', sort: true, width: 120}
            , {field: 'productCdesc', title: '产品中文描述', sort: true, width: 120}
            , {field: 'productEdesc', title: '产品英文描述', sort: true, width: 120}

        ]]
        , id: 'offertableid'
        , height: $(document).height() - $('#offerselect').offset().top - 50
        , page: true
        , limit: 300
        , limits: [300, 600, 900]
    });

    table.on('tool(offertable)', function(obj){
        let data = obj.data;
        let id = data.id;
        if (obj.event === "quotationno"){
            parent.intab("views/jsp/WarehouseManagement/tpl/viewinitializethe.jsp?id="+id,"初始化明细")
        }
    });


    form.on('submit(submitform)', function (data) {
        if ( data.field.operationCode =="" ||  data.field.operationCode==null){
            data.field.operationCode=user.code
        }
        table.reload('offertableid', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , url: 'initStock/statistics/getInitStatisticsByPage'
            , where: data.field
        }, 'data');
        return false;
    });


    form.on('submit(collectionExcel)', function (data) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: data.field,
            url:'initStock/export/initStatisticsExport',
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 120,
                        'AJ': 120
                    }, 120);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '初始化统计.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });


    document.getElementById('clear').onclick = function(){
        warehouseList.setValue([ ]);
        operationList.setValue([ ]);
    };

});